﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Base.WFLib;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;

namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        int? addLogicState(string pid, Guid instanceID, string departmentNo, string dutyNo, string userNo, string type, string submitDescription);

        [System.ServiceModel.OperationContract]
        int? childProcessRegress(string pid);


    }
    public partial class WorkflowService : IWorkflowService
    {
        //[3.1	工作流启动后]
        public int? afterWFStarted(string pid, 状态机模板 stateMachineTemplateActivity)
        {

            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_Instance wf_Instance = null;
                //[1]
                if (stateMachineTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFStarted()[1]出错！");
                    throw new Exception("WorkflowControl.afterWFStarted()[1]出错！");
                }

                //[2]
                if (mywfdb.WF_Instance.Count(p => p.instanceID == stateMachineTemplateActivity.流程编号) != 0)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFStarted()[2]出错！");
                    throw new Exception("WorkflowControl.afterWFStarted()[2]出错！");
                }
                else
                {

                    wf_Instance = new WF_Instance();
                    //	基本信息	
                    wf_Instance.ID = Guid.NewGuid();
                    wf_Instance.templateID = stateMachineTemplateActivity.模板编号;
                    wf_Instance.templateName = stateMachineTemplateActivity.模板名称;
                    wf_Instance.templateDescription = stateMachineTemplateActivity.模板说明;
                    wf_Instance.expandData = stateMachineTemplateActivity.扩展数据;
                    wf_Instance.templateType = stateMachineTemplateActivity.模板类型;
                    wf_Instance.templateEdition = stateMachineTemplateActivity.模板版本;
                    wf_Instance.isFreeze = 0;
                    //	表单配置		　
                    wf_Instance.startWindow = stateMachineTemplateActivity.启动窗体;
                    wf_Instance.dataFormList = stateMachineTemplateActivity.数据表单列表;
                    wf_Instance.startDataForm = stateMachineTemplateActivity.启动时填写的表单;
                    //	权限信息		　
                    wf_Instance.manageDepartment = stateMachineTemplateActivity.管理部门;
                    wf_Instance.manageDuty = stateMachineTemplateActivity.管理职能;
                    wf_Instance.manageUser = stateMachineTemplateActivity.管理人员;
                    wf_Instance.seeDepartment = stateMachineTemplateActivity.查看部门;
                    wf_Instance.seeDuty = stateMachineTemplateActivity.查看职能;
                    wf_Instance.seeUser = stateMachineTemplateActivity.查看人员;
                    wf_Instance.startUserPower = stateMachineTemplateActivity.启动人员特殊权限;
                    wf_Instance.powerID = stateMachineTemplateActivity.权限编号;
                    //	时限		　
                    wf_Instance.transactTimelimit = stateMachineTemplateActivity.办理时限;
                    wf_Instance.overtimeOperate = stateMachineTemplateActivity.超时操作;
                    //	业务信息(启动时)		　
                    wf_Instance.businessType = stateMachineTemplateActivity.业务类型;
                    wf_Instance.businessID = stateMachineTemplateActivity.业务编号;
                    wf_Instance.masterBusinessID = stateMachineTemplateActivity.主业务编号;
                    wf_Instance.businessName = stateMachineTemplateActivity.业务名称;
                    wf_Instance.businessDescription = stateMachineTemplateActivity.业务描述;
                    wf_Instance.urgency = stateMachineTemplateActivity.业务紧急度;
                    wf_Instance.secrecy = stateMachineTemplateActivity.业务保密度;
                    wf_Instance.startUser = stateMachineTemplateActivity.启动人员;
                    wf_Instance.startDepartment = stateMachineTemplateActivity.启动部门;
                    wf_Instance.startDuty = stateMachineTemplateActivity.启动职能;
                    wf_Instance.startTime = stateMachineTemplateActivity.启动时间;
                    //	子流程		　
                    wf_Instance.regressGroup = stateMachineTemplateActivity.流程回归组;
                    wf_Instance.regress = stateMachineTemplateActivity.回归主流程 ? 1 : 0;
                    wf_Instance.masterInstanceID = stateMachineTemplateActivity.主流程编号;
                    //	数据中转		　
                    wf_Instance.instanceID = stateMachineTemplateActivity.流程编号;
                    //	调试测试		　
                    wf_Instance.testID = stateMachineTemplateActivity.测试编号;
                    wf_Instance.stateTrack = stateMachineTemplateActivity.状态跟踪器;
                    //	引擎控制		　
                    wf_Instance.runState = "wait";
                    wf_Instance.completeTime = new DateTime(1900, 1, 1);
                    wf_Instance.residualTimelimit = 0;
                    mywfdb.WF_Instance.InsertOnSubmit(wf_Instance);

                    mywfdb.SubmitChanges();

                    return null;
                }
            }



        }

        //[3.2	工作流完成后]
        public int? afterWFFinished(string pid, 状态机模板 stateMachineTemplateActivity)
        {
            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_Instance wf_Instance = null;

                //[1]
                if (stateMachineTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFFinished()[1]出错！");
                    throw new Exception("WorkflowControl.afterWFFinished()[1]出错！");
                }

                //[2]
                if (mywfdb.WF_Instance.Count(p => p.instanceID == stateMachineTemplateActivity.流程编号) == 1)
                {
                    wf_Instance = mywfdb.WF_Instance.SingleOrDefault(p => p.instanceID == stateMachineTemplateActivity.流程编号);
                    //	引擎控制		　
                    wf_Instance.runState = "end";
                    wf_Instance.completeTime = DateTime.Now;
                    wf_Instance.residualTimelimit = 0;
                    mywfdb.SubmitChanges();
                    return null;
                }
                else
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFFinished()[2]出错！");
                    throw new Exception("WorkflowControl.afterWFFinished()[2]出错！");
                }
            }
        }

        //[3.3	工作流挂起前]
        public int? beforeWFWaited(string pid, 等待提交 waitSubmitTemplateActivity, Guid instanceID)
        {
            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_InstanceState wf_InstanceState = null;
                //[1]
                if (waitSubmitTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.beforeWFWaited()[1]出错！");
                    throw new Exception("WorkflowControl.beforeWFWaited()[1]出错！");
                }

                //[2]
                if (instanceID == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.beforeWFWaited()[2]出错！");
                    throw new Exception("WorkflowControl.beforeWFWaited()[2]出错！");
                }

                //[3]
                if (mywfdb.WF_InstanceState.Count(p => p.stateInstanceID == waitSubmitTemplateActivity.状态实例编号) != 0)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.beforeWFWaited()[3]出错！");
                    throw new Exception("WorkflowControl.beforeWFWaited()[3]出错！");
                }
                else
                {
                    wf_InstanceState = new WF_InstanceState();
                    wf_InstanceState.instanceID = instanceID;
                    //	基本信息		　
                    wf_InstanceState.stateID = waitSubmitTemplateActivity.状态编号;
                    wf_InstanceState.stateName = waitSubmitTemplateActivity.状态名称;
                    wf_InstanceState.stateDescription = waitSubmitTemplateActivity.状态说明;
                    wf_InstanceState.transactType = waitSubmitTemplateActivity.处理方式;
                    wf_InstanceState.powerID = waitSubmitTemplateActivity.权限编号;
                    wf_InstanceState.expandData = waitSubmitTemplateActivity.扩展数据;
                    wf_InstanceState.createTime = System.DateTime.Now;
                    //	表单配置		　
                    wf_InstanceState.startWindow = waitSubmitTemplateActivity.启动窗体;
                    wf_InstanceState.dataFormType = waitSubmitTemplateActivity.数据表单类型;
                    wf_InstanceState.dataExpression = waitSubmitTemplateActivity.数据分流表达式;
                    //	办理权限		　
                    wf_InstanceState.transactUser = waitSubmitTemplateActivity.办理人员;
                    wf_InstanceState.transactPower = waitSubmitTemplateActivity.办理时特殊权限;
                    wf_InstanceState.transactTimelimit = waitSubmitTemplateActivity.办理时限;
                    wf_InstanceState.transactOvertimeOperate = waitSubmitTemplateActivity.办理超时操作;
                    wf_InstanceState.transactOption = waitSubmitTemplateActivity.办理提交选项;
                    wf_InstanceState.transactSeeForm = waitSubmitTemplateActivity.办理查看业务表单;
                    wf_InstanceState.transactWriteForm = waitSubmitTemplateActivity.办理添写业务表单;
                    wf_InstanceState.transactBegintime = System.DateTime.Now;
                    wf_InstanceState.transactResidualTimelimit = 0;
                    //	接件权限		　
                    wf_InstanceState.inceptDepartment = waitSubmitTemplateActivity.接件部门;
                    wf_InstanceState.inceptDuty = waitSubmitTemplateActivity.接件职能;
                    wf_InstanceState.inceptPower = waitSubmitTemplateActivity.接件时特殊权限;
                    wf_InstanceState.inceptTimelimit = waitSubmitTemplateActivity.接件时限;
                    wf_InstanceState.inceptOvertimeOperate = waitSubmitTemplateActivity.接件超时操作;
                    wf_InstanceState.inceptResidualTimelimit = 0;
                    //	分配权限		　
                    wf_InstanceState.assignDepartment = waitSubmitTemplateActivity.分配部门;
                    wf_InstanceState.assignDuty = waitSubmitTemplateActivity.分配职能;
                    wf_InstanceState.assignUser = waitSubmitTemplateActivity.分配人员;
                    wf_InstanceState.assignPower = waitSubmitTemplateActivity.分配时特殊权限;
                    wf_InstanceState.assignTimelimit = waitSubmitTemplateActivity.分配时限;
                    wf_InstanceState.assignOvertimeOperate = waitSubmitTemplateActivity.分配超时操作;
                    wf_InstanceState.assignResidualTimelimit = 0;
                    //	管理权限		　
                    wf_InstanceState.manageDepartment = waitSubmitTemplateActivity.管理部门;
                    wf_InstanceState.manageDuty = waitSubmitTemplateActivity.管理职能;
                    wf_InstanceState.manageUser = waitSubmitTemplateActivity.管理人员;
                    wf_InstanceState.managePower = waitSubmitTemplateActivity.管理时特殊权限;
                    //	查看权限		　
                    wf_InstanceState.seeDepartment = waitSubmitTemplateActivity.查看部门;
                    wf_InstanceState.seeDuty = waitSubmitTemplateActivity.查看职能;
                    wf_InstanceState.seeUser = waitSubmitTemplateActivity.查看人员;
                    wf_InstanceState.seePower = waitSubmitTemplateActivity.查看时特殊权限;
                    //	动态域		　
                    wf_InstanceState.dynamicDepartment = waitSubmitTemplateActivity.动态域部门;
                    wf_InstanceState.dynamicDuty = waitSubmitTemplateActivity.动态域职能;
                    wf_InstanceState.dynamicUser = waitSubmitTemplateActivity.动态域人员;
                    wf_InstanceState.dynamicPower = waitSubmitTemplateActivity.动态域特殊权限;
                    wf_InstanceState.dynamicTimelimit = waitSubmitTemplateActivity.动态域时限;
                    wf_InstanceState.dynamicType = waitSubmitTemplateActivity.动态域类型;
                    //	提交上下文		　
                    wf_InstanceState.submitPower = waitSubmitTemplateActivity.提交人员特殊权限;
                    wf_InstanceState.submitDegree = waitSubmitTemplateActivity.提交次数;
                    wf_InstanceState.previousStateInstanceID = waitSubmitTemplateActivity.前一状态实例编号;
                    //	提交		　
                    wf_InstanceState.submitResult = waitSubmitTemplateActivity.提交结果;
                    wf_InstanceState.submitUser = waitSubmitTemplateActivity.提交人员;
                    wf_InstanceState.submitDepartment = waitSubmitTemplateActivity.提交部门;
                    wf_InstanceState.submitDuty = waitSubmitTemplateActivity.提交职能;
                    wf_InstanceState.submitTime = DateTime.Now;
                    wf_InstanceState.submitMode = waitSubmitTemplateActivity.提交方式;
                    wf_InstanceState.submitDescription = waitSubmitTemplateActivity.提交说明;
                    wf_InstanceState.triggerType = waitSubmitTemplateActivity.触发器类型;
                    wf_InstanceState.nextStateUser = waitSubmitTemplateActivity.下一状态办理人员;
                    wf_InstanceState.dataForm = waitSubmitTemplateActivity.数据表单;
                    //wf_InstanceState.transactTimelimit = waitSubmitTemplateActivity.下一状态办理时限;
                    //	数据中转		　
                    wf_InstanceState.stateInstanceID = waitSubmitTemplateActivity.状态实例编号;
                    //	调试测试		　
                    wf_InstanceState.debuger = waitSubmitTemplateActivity.调试器;
                    wf_InstanceState.stateTrack = waitSubmitTemplateActivity.状态跟踪器;
                    //	引擎控制		　
                    wf_InstanceState.runState = "wait";
                    wf_InstanceState.completeTime = DateTime.Now.AddDays(7);
                    mywfdb.WF_InstanceState.InsertOnSubmit(wf_InstanceState);
                    mywfdb.SubmitChanges();
                    return null;
                }
            }
        }

        //[3.4	工作流挂起后]
        public int? afterWFWaited(string pid, 等待提交 waitSubmitTemplateActivity, Guid instanceID)
        {
            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_InstanceState wf_InstanceState = null;
                //[1]
                if (waitSubmitTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFWaited()[1]出错！");
                    throw new Exception("WorkflowControl.afterWFWaited()[1]出错！");
                }

                //[2]
                if (instanceID == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFWaited()[2]出错！");
                    throw new Exception("WorkflowControl.afterWFWaited()[2]出错！");
                }

                //[3]
                if (mywfdb.WF_InstanceState.Count(p => p.stateInstanceID == waitSubmitTemplateActivity.状态实例编号 && p.runState == "wait") == 1)
                {
                    wf_InstanceState = mywfdb.WF_InstanceState.Single(p => p.stateInstanceID == waitSubmitTemplateActivity.状态实例编号 && p.runState == "wait");
                    //	提交		　
                    wf_InstanceState.submitResult = waitSubmitTemplateActivity.提交结果;
                    wf_InstanceState.submitUser = waitSubmitTemplateActivity.提交人员;
                    wf_InstanceState.submitDepartment = waitSubmitTemplateActivity.提交部门;
                    wf_InstanceState.submitDuty = waitSubmitTemplateActivity.提交职能;
                    wf_InstanceState.submitTime = waitSubmitTemplateActivity.提交日期;
                    wf_InstanceState.submitMode = waitSubmitTemplateActivity.提交方式;
                    wf_InstanceState.submitDescription = waitSubmitTemplateActivity.提交说明;
                    wf_InstanceState.triggerType = waitSubmitTemplateActivity.触发器类型;
                    wf_InstanceState.nextStateUser = waitSubmitTemplateActivity.下一状态办理人员;
                    //wf_InstanceState.transactTimelimit = waitSubmitTemplateActivity.下一状态办理时限;
                    wf_InstanceState.dataForm = waitSubmitTemplateActivity.数据表单;
                    //	引擎控制		　
                    wf_InstanceState.runState = "end";
                    wf_InstanceState.completeTime = DateTime.Now;
                    mywfdb.SubmitChanges();
                    return null;
                }
                else
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFWaited()[3]出错！状态实例编号不存在或状态不是[wait]！");
                    throw new Exception("WorkflowControl.afterWFWaited()[3]出错！状态实例编号不存在或状态不是[wait]！");
                }
            }
        }


        //[3.5 将已完成的状态信息移到历史表]
        public int? saveInstanceInfo(string pid)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[0]代表成功；[1]代表失败
                return wfDB.saveInstanceInfo();
            }
        }

        //[3.6	添加逻辑状态]
        public int? addLogicState(string pid, Guid instanceID, string departmentNo, string dutyNo, string userNo, string type, string submitDescription)
        {
            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {

                //[1]
                if (instanceID == Guid.Empty || string.IsNullOrEmpty(type))
                {
                    return 2;
                }
                //--

                WF_InstanceState wfState = new WF_InstanceState();

                wfState.stateInstanceID = Guid.NewGuid();


                wfState.instanceID = instanceID;
                wfState.submitUser = userNo;
                wfState.submitDepartment = departmentNo;
                wfState.submitDuty = dutyNo;
                wfState.submitDescription = submitDescription;
                wfState.transactUser = userNo;
                wfState.stateName = type;
                wfState.triggerType = type;
                wfState.submitResult = type;
                wfState.stateID = type;
                wfState.transactOption = type;
                wfState.createTime = DateTime.Now;

                wfState.stateDescription = "";
                wfState.transactType = "";
                wfState.powerID = "";
                wfState.expandData = "";
                wfState.startWindow = "";
                wfState.dataFormType = "";
                wfState.dataExpression = "";
                wfState.transactPower = "";
                wfState.transactTimelimit = 0;
                wfState.transactOvertimeOperate = "";
                wfState.transactSeeForm = "";
                wfState.transactWriteForm = "";
                wfState.transactBegintime = DateTime.Now;
                wfState.transactResidualTimelimit = 0;
                wfState.inceptDepartment = "";
                wfState.inceptDuty = "";
                wfState.inceptPower = "";
                wfState.inceptTimelimit = 0;
                wfState.inceptOvertimeOperate = "";
                wfState.inceptResidualTimelimit = 0;
                wfState.assignDepartment = "";
                wfState.assignDuty = "";
                wfState.assignUser = "";
                wfState.assignPower = "";
                wfState.assignTimelimit = 0;
                wfState.assignOvertimeOperate = "";
                wfState.assignResidualTimelimit = 0;
                wfState.manageDepartment = "";
                wfState.manageDuty = "";
                wfState.manageUser = "";
                wfState.managePower = "";
                wfState.seeDepartment = "";
                wfState.seeDuty = "";
                wfState.seeUser = "";
                wfState.seePower = "";
                wfState.dynamicDepartment = "";
                wfState.dynamicDuty = "";
                wfState.dynamicUser = "";
                wfState.dynamicPower = "";
                wfState.dynamicTimelimit = 0;
                wfState.dynamicType = "";
                wfState.submitPower = "";
                wfState.submitDegree = 0;
                wfState.previousStateInstanceID = Guid.Empty;
                wfState.submitTime = DateTime.Now;
                wfState.submitMode = "";
                wfState.nextStateUser = "";
                wfState.dataForm = "";
                wfState.debuger = "";
                wfState.stateTrack = 0;
                wfState.runState = "end";
                wfState.completeTime = DateTime.Now;

                mywfdb.WF_InstanceState.InsertOnSubmit(wfState);
                mywfdb.SubmitChanges();
                return null;

            }

        }


        //[3.7	子流程回归]
        public int? childProcessRegress(string pid)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (wfDB.WF_ChildRegress.Count() != 0)
                {
                    //[2]
                    List<Guid> masterInstanceIDs = wfDB.WF_ChildRegress.Select(p => p.masterInstanceID).Distinct().ToList();
                    foreach (Guid g in masterInstanceIDs)
                    {
                        var children = from child in wfDB.WF_ChildRegress where child.masterInstanceID == g select child;
                        var instances = from instance in wfDB.WF_Instance_V
                                        join child in children
                                            on instance.instanceID equals child.instanceID
                                        where instance.runState == "end"
                                        select instance;
                        //[2.1]
                        if (children.Count() == instances.Count())
                        {
                            var states = from instanceStates in wfDB.WF_InstanceState_V
                                         where instanceStates.instanceID == g
                                               && instanceStates.transactType == "子流程回归"
                                               && instanceStates.runState == "wait"
                                         select instanceStates;
                            if (states.Count() == 1)
                            {
                                WF_InstanceState_V wf_InstanceState_V = states.SingleOrDefault();

                                //[2.2]
                                if (wf_InstanceState_V.runState == "wait")
                                {
                                    SubmitResult submitResult = new SubmitResult();
                                    submitResult.instanceID = g;
                                    submitResult.stateInstanceID = wf_InstanceState_V.stateInstanceID;
                                    submitResult.submitResult = "子流程回归";
                                    submitResult.submitUser = "system";
                                    submitResult.submitDepartment = "system";
                                    submitResult.submitDuty = "system";
                                    submitResult.submitTime = DateTime.Now;
                                    submitResult.submitMode = "system";
                                    submitResult.submitDescription = "";
                                    submitResult.triggerType = "子流程回归";
                                    submitResult.nextStateUserNo = "";
                                    submitResult.dataForm = null;
                                    //[2.3]
                                    int? submitReturn = businessSubmit("", submitResult);
                                    if (submitReturn == null)
                                    {
                                        var children2 = from child in wfDB.WF_ChildRegress where child.masterInstanceID == g select child;
                                        foreach (WF_ChildRegress childRegress in children2.ToList())
                                        {
                                            wfDB.WF_ChildRegress.DeleteOnSubmit(childRegress);
                                        }
                                        try
                                        {
                                            wfDB.SubmitChanges();
                                        }
                                        catch (System.Exception ex)
                                        {
                                            MyLog.writeError(ex.Message);
                                        }
                                    }
                                    else
                                    {
                                        MyLog.writeError("子流程回归时提交业务，businessSubmit方法返回:[" + submitReturn.ToString() + "]。");
                                        throw new Exception("子流程回归时提交业务，businessSubmit方法返回:[" + submitReturn.ToString() + "]。");
                                    }
                                }
                                if (wf_InstanceState_V.runState == "end")
                                {
                                    //[2.4]
                                    var children3 = from child in wfDB.WF_ChildRegress where child.masterInstanceID == g select child;
                                    foreach (WF_ChildRegress childRegress in children3.ToList())
                                    {
                                        wfDB.WF_ChildRegress.DeleteOnSubmit(childRegress);
                                    }

                                    try
                                    {
                                        wfDB.SubmitChanges();
                                    }
                                    catch (System.Exception ex)
                                    {
                                        MyLog.writeError(ex.Message);
                                    }

                                }
                            }
                        }
                    }
                }
            }
            return null;
        }


        //[3.8*	工作流启动后]
        public int? afterWFStartedSequential(string pid, 顺序模板 sequentialTemplateActivity)
        {

            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_Instance wf_Instance = null;
                //[1]
                if (sequentialTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFStarted()[1]出错！");
                    throw new Exception("WorkflowControl.afterWFStarted()[1]出错！");
                }

                //[2]
                if (wfDB.WF_Instance.Count(p => p.instanceID == sequentialTemplateActivity.流程编号) != 0)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFStarted()[2]出错！");
                    throw new Exception("WorkflowControl.afterWFStarted()[2]出错！");
                }
                else
                {
                    wf_Instance = new WF_Instance();
                    //	基本信息	
                    wf_Instance.ID = Guid.NewGuid();
                    wf_Instance.templateID = sequentialTemplateActivity.模板编号;
                    wf_Instance.templateName = sequentialTemplateActivity.模板名称;
                    wf_Instance.templateDescription = sequentialTemplateActivity.模板说明;
                    wf_Instance.expandData = sequentialTemplateActivity.扩展数据;
                    wf_Instance.templateType = sequentialTemplateActivity.模板类型;
                    wf_Instance.templateEdition = sequentialTemplateActivity.模板版本;
                    //	表单配置		　
                    wf_Instance.startWindow = sequentialTemplateActivity.启动窗体;
                    wf_Instance.dataFormList = sequentialTemplateActivity.数据表单列表;
                    wf_Instance.startDataForm = sequentialTemplateActivity.启动时填写的表单;
                    //	权限信息		　
                    wf_Instance.manageDepartment = sequentialTemplateActivity.管理部门;
                    wf_Instance.manageDuty = sequentialTemplateActivity.管理职能;
                    wf_Instance.manageUser = sequentialTemplateActivity.管理人员;
                    wf_Instance.seeDepartment = sequentialTemplateActivity.查看部门;
                    wf_Instance.seeDuty = sequentialTemplateActivity.查看职能;
                    wf_Instance.seeUser = sequentialTemplateActivity.查看人员;
                    wf_Instance.startUserPower = sequentialTemplateActivity.启动人员特殊权限;
                    wf_Instance.powerID = sequentialTemplateActivity.权限编号;
                    //	时限		　
                    wf_Instance.transactTimelimit = sequentialTemplateActivity.办理时限;
                    wf_Instance.overtimeOperate = sequentialTemplateActivity.超时操作;
                    //	业务信息(启动时)		　
                    wf_Instance.businessType = sequentialTemplateActivity.业务类型;
                    wf_Instance.businessID = sequentialTemplateActivity.业务编号;
                    wf_Instance.masterBusinessID = sequentialTemplateActivity.主业务编号;
                    wf_Instance.businessName = sequentialTemplateActivity.业务名称;
                    wf_Instance.businessDescription = sequentialTemplateActivity.业务描述;
                    wf_Instance.urgency = sequentialTemplateActivity.业务紧急度;
                    wf_Instance.secrecy = sequentialTemplateActivity.业务保密度;
                    wf_Instance.startUser = sequentialTemplateActivity.启动人员;
                    wf_Instance.startDepartment = sequentialTemplateActivity.启动部门;
                    wf_Instance.startDuty = sequentialTemplateActivity.启动职能;
                    wf_Instance.startTime = sequentialTemplateActivity.启动时间;
                    //	子流程		　
                    wf_Instance.regressGroup = sequentialTemplateActivity.流程回归组;
                    wf_Instance.regress = sequentialTemplateActivity.回归主流程 ? 1 : 0;
                    wf_Instance.masterInstanceID = sequentialTemplateActivity.主流程编号;
                    //	数据中转		　
                    wf_Instance.instanceID = sequentialTemplateActivity.流程编号;
                    //	调试测试		　
                    wf_Instance.testID = sequentialTemplateActivity.测试编号;
                    wf_Instance.stateTrack = sequentialTemplateActivity.状态跟踪器;
                    //	引擎控制		　
                    wf_Instance.runState = "wait";
                    wf_Instance.completeTime = new DateTime(1900, 1, 1);
                    wf_Instance.residualTimelimit = 0;
                    wfDB.WF_Instance.InsertOnSubmit(wf_Instance);
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                    return null;
                }
            }

        }

        //[3.9*	工作流完成后]
        public int? afterWFFinishedSequential(string pid, 顺序模板 sequentialTemplateActivityTemplateActivity)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                WF_Instance wf_Instance = null;

                //[1]
                if (sequentialTemplateActivityTemplateActivity == null)
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFFinished()[1]出错！");
                    throw new Exception("WorkflowControl.afterWFFinished()[1]出错！");
                }

                //[2]
                if (wfDB.WF_Instance.Count(p => p.instanceID == sequentialTemplateActivityTemplateActivity.流程编号) == 1)
                {
                    wf_Instance = wfDB.WF_Instance.SingleOrDefault(p => p.instanceID == sequentialTemplateActivityTemplateActivity.流程编号);
                    //	引擎控制		　
                    wf_Instance.runState = "end";
                    wf_Instance.completeTime = DateTime.Now;
                    wf_Instance.residualTimelimit = 0;
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                    return null;
                }
                else
                {
                    MyLog.writeError("WorkflowEngineError:WorkflowControl.afterWFFinished()[2]出错！");
                    throw new Exception("WorkflowControl.afterWFFinished()[2]出错！");
                }
            }
        }

    }
}
